package com.wuliu.servlet;

import com.wuliu.dao.NewsClassDAO;
import com.wuliu.dao.NewsDAO;
import com.wuliu.dao.impl.NewsClassDAOImpl;
import com.wuliu.dao.impl.NewsDAOImpl;
import com.wuliu.model.News;
import com.wuliu.model.NewsClass;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/news/update")
public class AdminNewsUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewsClassDAO newsClassDAO=new NewsClassDAOImpl();
        List<NewsClass> newsClasses=newsClassDAO.findAll();
        req.setAttribute("newsClasses",newsClasses);
        int id=Integer.valueOf(req.getParameter("id"));
        NewsDAO newsDAO =new NewsDAOImpl();
        News news= newsDAO.findById(id);
//        System.out.println(news);
        req.setAttribute("news",news);
        req.getRequestDispatcher("/WEB-INF/view/admin/news_update.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id=Integer.valueOf(req.getParameter("id"));
        String time=req.getParameter("time");
        String name=req.getParameter("name");
        String desc=req.getParameter("desc");
        String source=req.getParameter("source");
        String author=req.getParameter("author");
        int views=Integer.valueOf(req.getParameter("views"));
        int cid=Integer.valueOf(req.getParameter("cid"));
        News news = new News(id,time,name,desc,source,author,views,cid);
        NewsDAO newsDAO=new NewsDAOImpl();
        newsDAO.update(news);
        resp.sendRedirect("/admin/news");
    }
}
